package com.paipai.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.paipai.dao.UserMapper;
import com.paipai.dto.RoundVo;
import com.paipai.entity.Round;
import com.paipai.entity.Saleroom;
import com.paipai.entity.User;
import com.paipai.es.RoundES;
import com.paipai.feign.RoundFeign;
import com.paipai.service.RoundService;
import com.paipai.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("round")
public class RoundController {
    @Autowired
    RoundService roundService;
    @Autowired
    private RoundFeign roundFeign;
    @Autowired
    private UserMapper userMapper;

    private User getUser(HttpServletRequest request){
        String token = request.getHeader("token");
        String account = JwtUtils.getAccount(token);
        User user = new User();
        user.setName(account);
        User singleUser = userMapper.selectOne(user);
        return singleUser;
    }

    @RequestMapping("roundPage")
    public PageInfo<RoundVo> roundPage(Integer pageSize, Integer pageNumber,String state){
        return  roundService.roundPage(pageSize,pageNumber,state);
    }
    @RequestMapping("roundES")
    public PageInfo<RoundES> roundESPageInfo(Integer pageSize, Integer pageNumber, String keyword){

        return  roundService.roundESPage(pageSize,pageNumber,keyword);
    }

    @RequestMapping("/publish")
    @ResponseBody
    public String publish(Long productId, String deposit, String startPrice, String stepPrice, String duration, Date startTime, HttpServletRequest request){
        //--获取当前用户
//        User user = UserUtils.getCurrUserFromHeader("token", User.class, request);
        User user = getUser(request);
        Long userId = user.getId();
        return roundService.publish(userId,productId,deposit,startPrice,stepPrice,duration,startTime);
    }
    @RequestMapping("getRoundById")
    public RoundVo getRoundById(long roundId){
        return roundService.getRoundById(roundId);
    }
}
